﻿package coreFx{
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.display.Sprite;
    import flash.text.*;
	
	public class State2 extends MovieClip{
		
		//Attrib
		var InQueue:Array;
		var staffList:Array;
		var margin:Number;
		
		
		var passCount:Number;
		public var txQueue:TextField;
		public var txPass:TextField;
		var format:TextFormat;
		
		
		
		public function State2(nStaff:Number,parentStage:Stage){
			
			trace("State2 Engage");
			InQueue = new Array();
			margin = -6;
			this.x = 300;this.y = 480;
			
			
			passCount=0;
			txQueue = new TextField();
			txPass = new TextField();			
			parentStage.addChild(txQueue);
			parentStage.addChild(txPass);
			
			txQueue.text = "0 In Queue";
			txQueue.x = this.x-160;
			txQueue.y = this.y+50;
			txQueue.width = 200;
			
			txPass.text = "0 has Passed";
			txPass.x = this.x-160;
			txPass.y = this.y+70;
			txPass.width = 200;
			
			format = new TextFormat();
			format.size = 26;
			txQueue.setTextFormat(format);
			txPass.setTextFormat(format);
			
			staffList = new Array();
			//-- Will set in loop later (prepare set of parameter in array)
			staffList[0] = new Staff("state2","state3",1000,this.x+200,this.y);
			staffList[1] = new Staff("state2","state3",1000,this.x+400,this.y);
			staffList[2] = new Staff("state2","state3",2000,this.x+600,this.y);
			
			parentStage.addChild(staffList[0]);
			parentStage.addChild(staffList[1]);
			parentStage.addChild(staffList[2]);
			
		}
		
		
		
		public function _push(cus:Customer){
			//play service MC
			cus.y = this.y;
		if(InQueue.length == 0){
				cus.x = this.x;
				//There is no customer before him
			}else{
				//cus.y -= (InQueue.length)*(cus.height/4);
				cus.x = InQueue[InQueue.length-1].x+margin;

			}
			
			
			InQueue.push(cus);
			txQueue.text = InQueue.length+" In Queue";
			txQueue.setTextFormat(format);
			//stop service MC 
		}
		
		public function _pop(){
			
			InQueue[0].y = this.y +10;//this.height +6;;
			InQueue[0].target = "state3";
			// you can also play cus mc Again
			
			InQueue.splice(0,1);
			
			
			for(var i =0;i<InQueue.length;i++)
			{
				InQueue[i].y = this.y;
				if(i == 0){
					InQueue[i].x = this.x;
				//There is no customer before him
				}else{
					//InQueue[i].y -= (InQueue.length)*(InQueue[i].height/4);
					InQueue[i].x = InQueue[i-1].x+margin;
				}
				
			}
			
			
			if(InQueue.length==0)gotoAndStop(0);
			//Halt State MC Playing
		}
		
		public function _popToChild():Customer{
			
			//InQueue[0].x = childxPos;//this.height +6;
			InQueue[0].target = "state2-1";
			// you can also play cus mc Again
			
			var customerTemp:Customer = InQueue[0];
			InQueue.splice(0,1);
			
			for(var i =0;i<InQueue.length;i++)
			{
				InQueue[i].y = this.y;
				if(i == 0){
					InQueue[i].x = this.x;
				//There is no customer before him
				}else{
					//InQueue[i].y -= (InQueue.length)*(InQueue[i].height/4);
					InQueue[i].x = InQueue[i-1].x+margin;
				}
				
			}
			
			
			if(InQueue.length==0)gotoAndStop(0);
			
			passCount++;
			txPass.text = passCount+" has Passed";
			txQueue.text = InQueue.length+" In Queue";
			txQueue.setTextFormat(format);
			txPass.setTextFormat(format);
			//Halt State MC Playing
	
			return customerTemp;
		}		
		
		
	}
}